<?php
namespace Api\Controller;

use Think\Controller;
class ZhiboController extends CommonController {
	public function index(){
		$eq['openid'] = I('openid');
		$map['wid'] = session('wid');
		$map['shop_id'] = M("Shop")->where($eq)->getField('shop_id');
		// $map['status'] = 1;
		// $map['start_date'] = array('gt', date("Y-m-d"));
		$list = M("Zhibo")->where($map)->select();
		$this->ajaxReturn($list);
	}

	public function getRtmp(){
		$map['id'] = I('id');
		$info = M("ZhiboSite")->where($map)->find();
		$this->ajaxReturn($info);
	}

	public function room(){
		$map['id'] = I('id');
		$info = M("Zhibo")->where($map)->find();
		if(!$info){
			return false;
		}

		$eq['wid'] = $info['wid'];
		$eq['id'] = array('in', $info['goods']);
		$info['goods_list'] = M("Goods")->where($eq)->select();

		$eq1['wid'] = $info['wid'];
		$eq1['id'] = $info['member_id'];
		$info['member'] = M("Member")->where($eq1)->find();

		$eq2['id'] = $info['rtmp_id'];
		$info['video_path'] = M("ZhiboSite")->where($eq2)->getField('play_url');

		$eq3['type'] = 'zhibo';
		$eq3['to'] = $info['member']['openid'];
		$eq3['is_pay'] = 1;
 		$info['gift_num'] = M("QuanGiftOrder")->where($eq3)->count();
		$info['order_num'] = 0;
		$info['member_num'] = count($this->rooms[$data['id']]);

		$eq4['openid'] = $data['openid'];
		$eq4['follow_openid'] = $info['member']['openid'];
		$info['is_follow'] = M("QuanFollow")->where($eq4)->getField('id');

		$eq5['type'] = 'zhibo';
		$eq5['qid'] = $data['id'];
        $eq5['openid'] = $data['openid'];
        $info['is_zan'] = M("QuanZanLog")->where($eq5)->getField('id');

		$res['data'] = $info;
		$res['type'] = 'info';
		$this->ajaxReturn($res);
	}

	public function add(){
		$eq['openid'] = I('openid');
		$member = M("Member")->where($eq)->find();
		$shop = M("Shop")->where($eq)->find();
		$goods = M("Goods")->where($eq)->getField("id",true);
		$goods_str = implode(',', $goods);

		$new['wid'] = $_SESSION['wid'];
		$new['member_id'] =  $member['mid'];
		$new['img'] = I('img');
		$new['shop_id'] = $shop['shop_id'];
		$new['title'] = I('title');
		$new['start_date'] = I('start_date');
		$new['start_time'] = I('start_time');
		$new['end_time'] = I('end_time');
		$new['status'] = 0;
		$new['create_time'] = date("Y-m-d H:i:s");
		$new['goods'] = $goods_str;
		if(M("Zhibo")->add($new)){
			$arr = array("err_code"=>0, "err_msg"=>"发布成功！");
		}else{
			$arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
		}
		$this->ajaxReturn($arr);
	}


	public function get_user_sign(){
		vendor('weixinDecrypt.TLSSigAPI');
		try{
		    $api = new \TLSSigAPI();
		    $api->SetAppid(1400068801);
		    // $private = file_get_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'ec_key.pem');
		    // $api->SetPrivateKey($private);
		    $public = file_get_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'public_key');
		    $api->SetPublicKey($public);
		    $sig = $api->genSig('user2');
		    $result = $api->verifySig($sig, 'user2', $init_time, $expire_time, $error_msg);
		    var_dump($result);
		    var_dump($init_time);
		    var_dump($expire_time);
		    var_dump($error_msg);
		}catch(Exception $e){
		    echo $e->getMessage();
		}
	}
}